package lty.pritace.topics;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 接受消息持久化
 * 
 * @author lty
 *
 */
public class PersisternceQueueReceiver {
	public static void main(String[] args) throws Exception {
		ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://192.168.233.128:61616");
		Connection connection = cf.createConnection();
		connection.setClientID("LTY3");

		final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
		Topic destination = session.createTopic("topic2");
		TopicSubscriber ts = session.createDurableSubscriber(destination, "NBA");
		connection.start();
		Message message = ts.receive();
		while (message != null) {
			// TextMessage message = (TextMessage) consumer.receive();
			TextMessage txt = (TextMessage) message;
			session.commit();
			System.out.println("收到消息1213：" + txt.getText());
			message = ts.receive(100000l);
		}
		session.close();
		connection.close();
	}
}
